#include "ScriptedPch.h"
#include "ScriptSystem.h"

#define GOSSIP_CHATID   818
#define GOSSIP_CHATWSG  799
#define GOSSIP_CHATAV   798
#define GOSSIP_CHATAB   781
#define GOSSIP_CHATEOTS 780

bool GossipHello_npc_currency_conversion(Player *pl, Creature *_c)
{
    if(pl->isInCombat())
        pl->CLOSE_GOSSIP_MENU();

    pl->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Exchange WSG Marks for PvP Tokens", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1);
    pl->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Exchange AV Marks for PvP Tokens", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+2);
    pl->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Exchange AB Marks for PvP Tokens", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+3);
    pl->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Exchange EOTS Marks for PvP Tokens", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+10);
    pl->PlayerTalkClass->SendGossipMenu(GOSSIP_CHATID, _c->GetGUID());

    return true;
}

bool GossipSelect_npc_currency_conversion(Player* pl, Creature* creature , uint32 uiSender, uint32 uiAction)
{
    switch(uiAction)
    {
    case GOSSIP_ACTION_INFO_DEF+1: // WSG MARKS
        {
            pl->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_CHAT, "Enter Amount of Marks", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+4, "", 0, true);
            pl->PlayerTalkClass->SendGossipMenu(GOSSIP_CHATWSG, creature->GetGUID());
            break;
        }
    case GOSSIP_ACTION_INFO_DEF+2: // AV MARKS
        {
            pl->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_CHAT, "Enter Amount of Marks", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+5, "", 0, true);
            pl->PlayerTalkClass->SendGossipMenu(GOSSIP_CHATAV, creature->GetGUID());
            break;
        }
    case GOSSIP_ACTION_INFO_DEF+3: //AB MARKS
        {
            pl->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_CHAT, "Enter Amount of Marks", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+6, "", 0, true);
            pl->PlayerTalkClass->SendGossipMenu(GOSSIP_CHATAB, creature->GetGUID());
            break;
        }
    case GOSSIP_ACTION_INFO_DEF+10: //EOTS MARKS
        {
            pl->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_CHAT, "Enter Amount of Marks", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+7, "", 0, true);
            pl->PlayerTalkClass->SendGossipMenu(GOSSIP_CHATEOTS, creature->GetGUID());
            break;
        }
    default:
        pl->CLOSE_GOSSIP_MENU();
    }
    return true;
}

bool GossipSelectWithCode_npc_currency_conversion_codebox(Player* pl, Creature* _c, uint32 uiSender, uint32 uiAction, const char* sCode)
{
    if(uiSender != GOSSIP_SENDER_MAIN)
        return true;

    switch(uiAction)
    {
    case GOSSIP_ACTION_INFO_DEF+4: // WSG MARKS (Conversion: 2 Marks = 1 PvP)
        {
            uint32 marks = atoi(sCode);
            if(!pl->HasItemCount(20558, marks))
            {
                _c->MonsterWhisper("You may only convert Marks that you actually possess.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            if(marks % 2)
            {
                _c->MonsterWhisper("You may only convert WSG Marks in multiples of 2. Please try again.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            uint32 pvpTokens = marks / 2;
            ItemPosCountVec dest;
            uint8 msg = pl->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, 29434, pvpTokens);
            if (msg == EQUIP_ERR_OK)
            {
                Item* item = pl->StoreNewItem(dest, 29434,true);
                if (item)
                {
                    pl->SendNewItem(item, pvpTokens ,false, true);
                    pl->DestroyItemCount(20558, marks, true, false);
                }
            }
            else
                pl->SendEquipError(msg,NULL,NULL);

            pl->CLOSE_GOSSIP_MENU();
            return true;
        }
    case GOSSIP_ACTION_INFO_DEF+5: // AV MARKS (Conversion: 1 Marks = 2 PvP)
        {
            uint32 marks = atoi(sCode);
            if(!pl->HasItemCount(20560, marks))
            {
                _c->MonsterWhisper("You may only convert Marks that you actually possess.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            ItemPosCountVec dest;
            uint8 msg = pl->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, 29434, 1);
            if (msg == EQUIP_ERR_OK)
            {
                Item* item = pl->StoreNewItem(dest, 29434,true);
                if (item)
                {
                    pl->SendNewItem(item, marks *2 ,false, true);
                    pl->DestroyItemCount(20560, marks, true, false);
                }
            }
            else
                pl->SendEquipError(msg,NULL,NULL);

            pl->CLOSE_GOSSIP_MENU();
            return true;
        }
    case GOSSIP_ACTION_INFO_DEF+6: // AB MARKS (Conversion: 1 Marks = 1 PvP)
        {
            uint32 marks = atoi(sCode);
            if(!pl->HasItemCount(20559, marks))
            {
                _c->MonsterWhisper("You may only convert Marks that you actually possess.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }
            if(marks % 1)
            {
                _c->MonsterWhisper("You may only convert AB Marks in multiples of 1. Please try again.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            uint32 pvpTokens = marks;

            ItemPosCountVec dest;
            uint8 msg = pl->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, 29434, pvpTokens);
            if (msg == EQUIP_ERR_OK)
            {
                Item* item = pl->StoreNewItem(dest, 29434,true);
                if (item)
                {
                    pl->SendNewItem(item, pvpTokens ,false, true);
                    pl->DestroyItemCount(20559, marks, true, false);
                }
            }
            else
                pl->SendEquipError(msg,NULL,NULL);

            pl->CLOSE_GOSSIP_MENU();
            return true;
        }
    case GOSSIP_ACTION_INFO_DEF+7: // EOTS MARKS (Conversion: 1 Marks = 1 PvP)
        {
            uint32 marks = atoi(sCode);
            if(!pl->HasItemCount(29024, marks))
            {
                _c->MonsterWhisper("You may only convert Marks that you actually possess.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }
            if(marks % 1)
            {
                _c->MonsterWhisper("You may only convert EOTS Marks in multiples of 1. Please try again.", pl->GetGUID());
                pl->CLOSE_GOSSIP_MENU();
                return true;
            }

            uint32 pvpTokens = marks;

            ItemPosCountVec dest;
            uint8 msg = pl->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, 29434, pvpTokens);
            if (msg == EQUIP_ERR_OK)
            {
                Item* item = pl->StoreNewItem(dest, 29434,true);
                if (item)
                {
                    pl->SendNewItem(item, pvpTokens ,false, true);
                    pl->DestroyItemCount(29024, marks, true, false);
                }
            }
            else
                pl->SendEquipError(msg,NULL,NULL);

            pl->CLOSE_GOSSIP_MENU();
            return true;
        }
    default:
        pl->CLOSE_GOSSIP_MENU();
    }
    return true;
}

void AddSC_currency_conversion()
{
    Script *newscript;

    newscript = new Script;
    newscript->Name="currency_conversion";
    newscript->pGossipHello =           &GossipHello_npc_currency_conversion;
    newscript->pGossipSelect =          &GossipSelect_npc_currency_conversion;
    newscript->pGossipSelectWithCode =  &GossipSelectWithCode_npc_currency_conversion_codebox;
    newscript->RegisterSelf();
}





